package com.htfyun.ft.viewmodel;

import android.app.Activity;
import android.content.Context;

import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;

public class ViewModelFTGot {

    public static ViewModelFT getViewModel(Context context) {
        return getViewModel(checkContext(context), ViewModelFT.class);
    }

    public static ViewModelFT getViewModel(Activity context) {
        return getViewModel(checkContext(context), ViewModelFT.class);
    }

    private static <T extends ViewModelFT> T getViewModel(FragmentActivity context, Class<T> tClass) {
        return ViewModelProviders.of(context).get(tClass);
    }

    private static FragmentActivity checkContext(Context context) {
        if (context instanceof FragmentActivity) return (FragmentActivity) context;
        throw new IllegalContextException();
    }

    public static class IllegalContextException extends RuntimeException {
        public IllegalContextException() {
            super("ViewModelGot need FragmentActivity as Context！");
        }
    }
}
